package com.example.gateway.filter;

import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Objects;

/**
 * @author Destinycc
 */
@Component("ipKeyResolver")
public class IpKeyResolver extends CustomKeyResolver {

    public static final String BEAN_NAME = "ipKeyResolver";

    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        return Mono.just(
                exchange.getRequest().getHeaders().getFirst("X-Forwarded-For") == null ?
                        Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getHostString() : Objects.requireNonNull(exchange.getRequest().getHeaders().getFirst("X-Forwarded-For"))
        );
    }

    @Override
    public String getBeanName(){
        return BEAN_NAME;
    }

}
